﻿using System;
using System.Linq;

namespace Tetris.Model
{
    public class ElementModel: IElementModel //to powinien byc klocek: zestaw kwadracikow
    {
        public ElementModel()
        {
            this.BlocksCollection = new IBlockModel[4];

            for (var j = 0; j < 4; j++)
            {
                this.BlocksCollection[j] = new BlockModel();
            }
        }
        
        public ElementModel(int type, int width, int height)
        {
            this.BlocksCollection = new IBlockModel[4];
            this.Type = type;
            this.BlockHeight = height;
            this.BlockWidth = width;
            
            for (var j = 0; j<4 ; j++)
            {
               this.BlocksCollection[j] = new BlockModel(); 
            }
        
            switch (type)
            {
                case 0:
                    this.CreateElementFirstType(width, height);
                    break;
                case 1:
                    this.CreateElementSecondType(width, height);
                    break;
                case 2:
                    this.CreateElementThirdType(width, height);
                    break;
                case 3:
                    this.CreateElementFourthType(width, height);
                    break;
                default:
                    break;

            }
        }


        #region IElementModel implementation

        public IBlockModel[] BlocksCollection
        {
            get; private set;
        }

        public void Rotate()
        {
            //odwroc klocek o 90 stopni
            int x = BlocksCollection[0].Left;
            int y = BlocksCollection[0].Top;

            foreach (var blockModel in BlocksCollection)
            {
                blockModel.Left -= x;
                blockModel.Top -= y;
            }
            
            for (int i = 1; i < BlocksCollection.Count(); i++ )
            {
                int tmp = BlocksCollection[i].Left;
                BlocksCollection[i].Left = -1 * BlocksCollection[i].Top;
                BlocksCollection[i].Top =  tmp;
            }

            foreach (var blockModel in BlocksCollection)
            {
                blockModel.Left += x;
                blockModel.Top += y;
            }
        }

        public void CopyFrom(IElementModel elementModel)
        {
            for(int i=0; i<elementModel.BlocksCollection.Count(); i++)
            {
                this.BlocksCollection[i].CopyFrom( elementModel.BlocksCollection[i] );
            }
        }

        public int Type
        {
            get; private set;
        }

        public int BlockHeight
        {
            get; private set;
        }

        public int BlockWidth
        {
            get; private set;
        }

        #endregion

        #region private methods

        private void CreateElementBasic(int width, int height)
        {
            for (int i = 0; i < 4; i++)
            {
                this.BlocksCollection[i].Height = height;
                this.BlocksCollection[i].Width = width;
                this.BlocksCollection[i].Left = i * width + 60;
                this.BlocksCollection[i].Top = 0;
            }
        }

        private void CreateElementFirstType(int width, int height)
        {
            this.CreateElementBasic(width, height);
        }

        private void CreateElementSecondType(int width, int height)
        {
            this.CreateElementBasic(width, height);
            this.BlocksCollection[3].Left -= width;
            this.BlocksCollection[3].Top += height;
        }

        private void CreateElementThirdType(int width, int height)
        {
            this.CreateElementBasic(width, height);
            this.BlocksCollection[3].Left = this.BlocksCollection[1].Left;
            this.BlocksCollection[3].Top = this.BlocksCollection[1].Top + height;
        }

        private void CreateElementFourthType(int width, int height)
        {
            this.CreateElementBasic(width, height);
            this.BlocksCollection[2].Left -= 2*width;
            this.BlocksCollection[2].Top += height;

            this.BlocksCollection[3].Left -= 2*width;
            this.BlocksCollection[3].Top += height;

        }

        #endregion

    }
}
